/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.dao.ProductDao;
import com.minierp.dao.ProductTypeDao;
import com.minierp.helper.EncCommonHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ProductTypeMACtr extends MultiActionController {
	private ProductTypeDao productTypeDao;
	private ProductDao productDao;
	private String listView;
	private String listForProductSelectView;

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listView);
		List list = this.productTypeDao.getList(true);
		mv.addObject("productTypeList", list);
		String defaultId = request.getParameter("default_id");
		if (defaultId != null && !defaultId.equals("")) {
			mv.addObject("defaultId", defaultId);
		}

		return mv;
	}

	public ModelAndView handleListForProductSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForProductSelectView);
		List list = this.productTypeDao.getList(true);
		mv.addObject("productTypeList", list);
		String defaultId = request.getParameter("default_id");
		if (defaultId != null && !defaultId.equals("")) {
			mv.addObject("defaultId", defaultId);
		}

		String type = request.getParameter("type");
		mv.addObject("type", type);
		mv.addObject("stock", request.getParameter("stock"));
		mv.addObject("stoId", request.getParameter("sto_id"));
		return mv;
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String returnUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + returnUrl);
		if (!EncCommonHelper.checkPrivilege(request, "BASE_PROD")) {
			throw new Exception("Permission deny!");
		} else {
			long id = Long.parseLong(request.getParameter("id"));
			int proNum = this.productDao.getCount(id);
			if (proNum > 0) {
				EncCommonHelper.setAlertMessage(request,
						"该类别或该类别的下属类别有材料存在，不能删除！");
			} else {
				this.productTypeDao.unenabledTree(id);
				EncCommonHelper.setAlertMessage(request, "删除成功！");
			}

			return mv;
		}
	}

	public ProductTypeDao getProductTypeDao() {
		return this.productTypeDao;
	}

	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public String getListForProductSelectView() {
		return this.listForProductSelectView;
	}

	public void setListForProductSelectView(String listForProductSelectView) {
		this.listForProductSelectView = listForProductSelectView;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
}